package com.xiaomage.slide

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * Created by Administrator on 2018/3/21 0021.
 */
class MyViewLikeImage4 : View {
    private var mPolyMatrix: Matrix? = null         // 测试setPolyToPoly用的Matrix
    private var bitmap: Bitmap?=null
    constructor(context: Context):super(context)
    constructor(context: Context, attributeSet: AttributeSet):super(context,attributeSet)
    val paint= Paint()
    var path: Path?=null
    var BmpDST: Bitmap?=null
    init {
        paint.setColor(Color.RED);  //设置画笔颜色
        paint.setStyle(Paint.Style.FILL_AND_STROKE);//填充样式改为描边
        paint.setStrokeWidth(5f);//设置画笔宽度
    }
    fun forImage1(imageHeight:Float,imageHeightMax:Float,imageWidth:Float,imageWidthMax:Float){
        //初始化控件1
        path = Path()
        path!!.moveTo(0f, (imageHeightMax-imageHeight)/2) //设定起始点
        path!!.lineTo(imageWidth, (imageHeightMax-imageHeight)/2)
        path!!.lineTo(imageWidth, (imageHeightMax-imageHeight)/2+imageHeight)
        path!!.lineTo(0f, (imageHeightMax-imageHeight)/2+imageHeight)
        path!!.close()//闭环
        postInvalidate()
    }
    fun setImageBitmap(b: Bitmap, width:Float, height:Float){
        bitmap=zoomImage(b,width,height)
        /*val h=b.height.toFloat()/ height
        val w=b.width/ width
        var minB= if(h>w){
            w
        }else{
            h
        }
        //设置图片缩放比例
        mPolyMatrix= Matrix()
        mPolyMatrix!!.postScale(b.width*minB,b.height*minB)
        bitmap= Bitmap.createBitmap(b,0,0,b.width,b.height,mPolyMatrix,true)*/

        BmpDST = Bitmap.createBitmap(width.toInt(), height.toInt(), Bitmap.Config.ARGB_8888);
        postInvalidate()
    }
    //路面
    //num最大：（150-112.5）/2=18.75
    //本来高宽比是：3/4
    //但由于宽度需要移动两次，而高度一次就移动完成了，所以：移动的宽度=(num*8/3)/2=num*4/3
    //宽度移动最大为=18.75*8/3/2=50/2=25
    fun set11(number: Float, imageHeight:Float, imageHeightMax:Float, imageWidth:Float, imageWidthMax:Float, keyWidth:Float){
        //initBitmapAndMatrix(number)
        val x=(number*(imageWidthMax-keyWidth))/((imageHeightMax-imageHeight)/2)
        path = Path()
        path!!.moveTo(0f, 0f) //设定起始点
        path!!.lineTo(imageWidthMax-x, number)
        path!!.lineTo(imageWidthMax-x, imageHeightMax-number)
        path!!.lineTo(0f, imageHeightMax)
        path!!.close()//闭环
        ForLog.showError("四个点的坐标：（0,0）；（" + (imageWidthMax - x) + "," + number + "）；（"
                + (imageWidthMax - x) + "," + (imageHeightMax - number) + "）；（0," + imageHeightMax + "）")
        postInvalidate()
    }
    //200-150=50/2=25+150=175--18.75*4/3
    fun set12(number: Float,imageHeight:Float,imageHeightMax:Float,imageWidth:Float,imageWidthMax:Float,keyWidth:Float){
        //initBitmapAndMatrix(number)
        val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        path = Path()
        path!!.moveTo(0f, number) //设定起始点
        path!!.lineTo(keyWidth-x,(imageHeightMax-imageHeight)/2)
        path!!.lineTo(keyWidth-x,(imageHeightMax-imageHeight)/2+imageHeight)
        path!!.lineTo(0f, imageHeightMax-2*number)
        path!!.close()//闭环
        postInvalidate()
    }
    //路面
    //num最大：（150-112.5）/2=18.75
    //本来高宽比是：3/4
    //但由于宽度需要移动两次，而高度一次就移动完成了，所以：移动的宽度=(num*8/3)/2=num*4/3
    //宽度移动最大为=18.75*8/3/2=50/2=25
    fun ser31(number: Float, imageHeight:Float, imageHeightMax:Float, imageWidth:Float, imageWidthMax:Float, keyWidth:Float){
        //initBitmapAndMatrix(number)
        val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        path = Path()
        path!!.moveTo(0f, (imageHeightMax-imageHeight)/2) //设定起始点
        path!!.lineTo(imageWidth+x, (imageHeightMax-imageHeight)/2-number)
        path!!.lineTo(imageWidth+x, (imageHeightMax-imageHeight)/2+imageHeight+number)
        path!!.lineTo(0f, (imageHeightMax-imageHeight)/2+imageHeight)
        path!!.close()//闭环
        postInvalidate()
    }
    //200-150=50/2=25+150=175--18.75*4/3
    fun set32(number: Float, imageHeight:Float, imageHeightMax:Float, imageWidth:Float, imageWidthMax:Float, keyWidth:Float,xMove:Float){
        //initBitmapAndMatrix(number)
        path = Path()
        path!!.moveTo(0f,(imageHeightMax-imageHeight)/2- number) //设定起始点
        path!!.lineTo(keyWidth+xMove, 0f)
        path!!.lineTo(keyWidth+xMove, imageHeightMax)
        path!!.lineTo(0f, (imageHeightMax-imageHeight)/2+imageHeight+number)
        path!!.close()//闭环
        postInvalidate()
    }
    //------------------------------------------------------------------------------
    fun set12_2(number: Float,imageHeight:Float,imageHeightMax:Float,imageWidth:Float,imageWidthMax:Float,keyWidth:Float,xMove:Float){
        //initBitmapAndMatrix(number)
        //val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        path = Path()
        path!!.moveTo(0f, number) //设定起始点
        path!!.lineTo(keyWidth+xMove,(imageHeightMax-imageHeight)/2)
        path!!.lineTo(keyWidth+xMove,(imageHeightMax-imageHeight)/2+imageHeight)
        path!!.lineTo(0f, imageHeightMax-number)
        path!!.close()//闭环
        postInvalidate()
    }
    fun ser31_2(number: Float, imageHeight:Float, imageHeightMax:Float, imageWidth:Float, imageWidthMax:Float, keyWidth:Float,xMove:Float){
        //initBitmapAndMatrix(number)
        //val x=(number*(keyWidth-imageWidth))/((imageHeightMax-imageHeight)/2)
        path = Path()
        path!!.moveTo(0f, (imageHeightMax-imageHeight)/2) //设定起始点
        path!!.lineTo(imageWidth-xMove, (imageHeightMax-imageHeight)/2-number)
        path!!.lineTo(imageWidth-xMove, (imageHeightMax-imageHeight)/2+imageHeight+number)
        path!!.lineTo(0f, (imageHeightMax-imageHeight)/2+imageHeight)
        path!!.close()//闭环
        postInvalidate()
    }
    //------------------------------------第二阶段3d操作------------------------------------------
    fun stateTwoChangeSmall(cw:Float,ch:Float,skewMaxHeight:Float,imageWidth:Float,imageHeight:Float){

        path = Path()
        path!!.moveTo(0f, skewMaxHeight+ch) //设定起始点
        path!!.lineTo(imageWidth-cw, skewMaxHeight+ch)
        path!!.lineTo(imageWidth-cw, skewMaxHeight+imageHeight-ch)
        path!!.lineTo(0f, skewMaxHeight+imageHeight-ch)
        path!!.close()//闭环
        postInvalidate()
    }
    fun stateTwoChangeBig(cw:Float,ch:Float,skewMaxHeight:Float,imageHeight:Float,stateTwoMixWidth:Float,stateTwoMixHeight:Float){
        path = Path()
        path!!.moveTo(0f,(imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight ) //设定起始点
        path!!.lineTo(stateTwoMixWidth+cw,(imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight)
        path!!.lineTo(stateTwoMixWidth+cw, (imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight+ch*2+stateTwoMixHeight)
        path!!.lineTo(0f, (imageHeight-stateTwoMixHeight)/2-ch+skewMaxHeight+ch*2+stateTwoMixHeight )
        path!!.close()//闭环
        postInvalidate()
    }
    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        /*if(bitmap!=null){
            if(rect!=null){
                // 根据Matrix绘制一个变换后的图片
                canvas.drawPath()
                canvas.drawBitmap(bitmap, mPolyMatrix, null);
            }else {
                canvas.drawBitmap(bitmap, null, Rect(0, 0, width, height), paint)
            }
        }*/
        if(path!=null){
            //先清空bitmap上的图像,然后再画上Path
            //val c = Canvas(BmpDST)
           // c.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR)
            //canvas.drawPath(path, paint)
            //先清空bitmap上的图像,然后再画上Path
            val c = Canvas(BmpDST)
            c.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR)
            c.drawPath(path, paint)

           // canvas.drawBitmap(bitmap, 0f, 0f, paint)
            val layerId = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null, Canvas.ALL_SAVE_FLAG)
            canvas.drawBitmap(bitmap, 0f, 0f, paint)
            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
            canvas.drawBitmap(BmpDST, 0f, 0f, paint)
            paint.xfermode = null
            canvas.restoreToCount(layerId)
        }else{
            if(bitmap!=null){
                canvas.drawBitmap(bitmap, null, Rect(0, 0, width, height), paint)
            }
        }
        /*val layerId = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null, Canvas.ALL_SAVE_FLAG)
        canvas.drawBitmap(BmpDST, 0, 0, paint)
        mBitPaint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP))
        canvas.drawBitmap(BmpSRC, 0, 0, paint)
        mBitPaint.setXfermode(null)
        canvas.restoreToCount(layerId)*/
    }

    fun zoomImage(bgimage: Bitmap, newWidth: Float,
                  newHeight: Float): Bitmap {
        // 获取这个图片的宽和高
        val width = bgimage.width.toFloat()
        val height = bgimage.height.toFloat()
        // 创建操作图片用的matrix对象
        val matrix = Matrix()
        // 计算宽高缩放率
        val scaleWidth = newWidth.toFloat() / width
        val scaleHeight = newHeight.toFloat() / height
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight)
        return Bitmap.createBitmap(bgimage, 0, 0, width.toInt(),
                height.toInt(), matrix, true)
    }
}